<?php

namespace admin;

use \core\Controller;

class Type extends Controller
{
    public function __construct()
    {
        parent::__construct();

        check_login();
    }

    public function add()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $pid = session('datalist.pid') ?? false;
            if ($pid === false)
                exit(json_return('非法操作!'));

            $sort = sqlCol('select max(sort) from bg_type where pid='.$pid) ?? 0;
            if ($pid === 0)
                $path = ',,';
            $path = sqlCol('select path from bg_type where id='.$pid);
            $path = $path === ',,' ? ','.$pid.',' : $path.$pid.',';

            $ret = M('bg_type')->insert([
                'tag' => $_POST['tag'],
                'pid' => $pid,
                'name' => $_POST['name'],
                'path' => $path,
                'sort' => ++$sort
            ]);

            if (!$ret)
                exit(json_return('操作失败!'));

            exit(json_return([
                'close' => true,
                'msg' => '操作成功!',
                'url' => U('Type/datalist'),
                'tag' => "#datalist"
            ]));
        }
        $this->assign('optxt', '添加');
        $parent = session("datalist.typename");
        $this->assign('parent', $parent);
        $this->assign('row', ['id' => '', 'tag' => '', 'name' => '']);
        echo $this->fetch();
    }

    public function edit($id = null)
    {
        if (IS_POST) {
            if (!isset($_POST['update_id'])
                    || $_POST['update_id'] === '') {
                exit(json_return('非法操作!'));
            }

            $ret = M('bg_type')->update([
                'tag' => $_POST['tag'],
                'name' => $_POST['name'],
                'mtime' => date('Y-m-d H:i:s')
            ], "id=".$_POST['update_id']);

            if (!$ret)
                exit(json_return('操作失败!'));

            exit(json_return('操作成功!', U('Type/datalist'), "#datalist", true));
        }
        if ($id === null)
            exit(json_return('非法操作!'));

        $this->assign('optxt', '编辑');
        $parent = session("datalist.typename");
        $this->assign('parent', $parent);
        $this->assign('row', sqlRow('select * from bg_type where id='.$id));
        echo $this->fetch('add.tpl');
    }

    public function del($id)
    {
        $ret = M('bg_type')->del("id=".$id);
        echo json_return('删除成功!', U('Type/datalist'), "#datalist");
    }

    public function subType($pid = null, $page = null)
    {
        $name = "jsdf";
        $listID = 'subtypelist'.$pid;
        $total = sqlCol('select count(*) from bg_type where pid='.$pid);
        $pageArray = page_fetch($total, U().'/'.$pid, $page, '#'.$listID, 3);

        $data = $this->_datalist($pid, $pageArray, $name, ['close_top_page' => true]);
        if ($page != null)
            exit(json_return_data($data));

        $this->assign('listID', $listID);
        $this->assign('html', $data);
        echo $this->fetch();
    }

    private function _datalist($pid, $pageArray, $name = "", $opt = [])
    {
        $sql = 'select * from bg_type where pid='.$pid.$pageArray['limit'];
        if (isset($opt['close_top_page']))
            $this->assign('close_top_page', $opt['close_top_page']);
        $this->assign('name', $name);
        $this->assign('page', $pageArray['html']);
        $this->assign('data', sqlAll($sql));
        return $this->fetch('datalist.tpl');
    }

    public function datalist($page = null, $pid = null, $name = "顶级类型")
    {
        if ($pid === null) {
            $pid = session('datalist.pid');
            $name = session('datalist.typename');
        } else {
            session('datalist.pid', $pid);
            session('datalist.typename', $name);
        }

        $total = sqlCol('select count(*) from bg_type where pid='.$pid);
        $pageArray = page_fetch($total, U(), $page);
        $data = $this->_datalist($pid, $pageArray, $name);

        if (IS_AJAX)
            echo json_return_data($data);
        else
            return $data;
    }
}
